﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Web.UI;
using Lswweb.Domain;
using Lswweb.Model;

namespace Lswweb.Controls
{
    /// <summary>
    /// 使用JQuery.Accordion UI展示
    /// theme方面多了一个.ui-accordion-content-item用来控制二级栏目的样式
    /// .ui-accordion-content-item .ui-state-active / .ui-accordion-content-item .ui-state-hover
    /// </summary>
    public class AccordionMenu : Control,INamingContainer
    {
        public override void RenderControl(HtmlTextWriter writer)
        {
            if (!string.IsNullOrEmpty(ThemeCss))
                writer.AddAttribute(HtmlTextWriterAttribute.Class, ThemeCss);
            writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            writer.RenderBeginTag(HtmlTextWriterTag.Div);

            int index = 0;
            IList<MapNode> list = GetNodes(string.Empty);
            MapNode currentMapNode=SiteContext.Current.MapNode;
            string[] currentPaths = currentMapNode == null ? new string[0] : currentMapNode.Path.Split('/');
            foreach (MapNode node in list)
            {
                if (currentPaths.Contains(node.Path))
                    index = list.IndexOf(node);

                RenderItem(writer, node);
            }

            writer.RenderEndTag();

            RenderScript(writer,index);
        }

        private void RenderScript(HtmlTextWriter writer, int activeIndex)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script language=\"javascript\" type=\"text/javascript\">");
            script.Append("$(function(){$(\"#" + this.ClientID + "\").tabs(\"ul\", { tabs: 'h3', effect: 'slide',initialIndex:" + activeIndex + " });});");
            script.Append("</script>");

            writer.Write(script.ToString());
        }

        private void RenderItem(HtmlTextWriter writer, MapNode mapNode)
        {
            if (mapNode == null || !mapNode.Visible)
                return;

            RenderHeader(writer,mapNode.Url,mapNode.Text);
            RenderItemContent(writer,mapNode.Name);
        }
        private void RenderHeader(HtmlTextWriter writer,string url,string text)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.H3);
            RenderLinkControl(writer, url, text);
            writer.RenderEndTag();
        }
        private void RenderItemContent(HtmlTextWriter writer, string nodeParentName)
        {
            IList<MapNode> list = GetNodes(nodeParentName);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            foreach (MapNode node in list)
            {
                if (node == null || !node.Visible)
                    continue;

                if (SiteContext.Current.MapNode != null && SiteContext.Current.MapNode.Path == node.Path)
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "currentItem");

                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                RenderLinkControl(writer, node.Url, node.Text);
                writer.RenderEndTag();
            }

            if (list.Count == 0)
            {
                writer.RenderBeginTag(HtmlTextWriterTag.Li);
                writer.Write("&nbsp;");
                writer.RenderEndTag();
            }
                
            writer.RenderEndTag();
        }
        private void RenderLinkControl(HtmlTextWriter writer, string url,string text)
        {
            if (!string.IsNullOrEmpty(url))
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "document.location.href=this.href;");
                writer.AddAttribute(HtmlTextWriterAttribute.Href, url);
            }
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(text);
            writer.RenderEndTag();
        }

        private IList<MapNode> GetNodes(string parentNodeName)
        {
            return Maps.Content.GetChildNodes(MenuMapType, parentNodeName,info=>info.Visible);
        }

        #region 公共属性

        public string ThemeCss { get; set; }

        public MapType MenuMapType { get; set; }

        #endregion
    }
}
